package main

import (
	"fmt"
	"rpc"
	"time"
)

type Args struct {
	A int
	B int
}

func (a *Args) Add(arg *Args, b *int) error {
	*b = arg.A + arg.B
	return nil
}

func (a *Args) ByCall(arg *Args, _ *int8) error {
	a.A = arg.A
	a.B = arg.B
	fmt.Println("by call")
	return nil
}

func (a *Args)callback(ret interface{}, err error) {
	val, _ := ret.(*int)
	fmt.Println(a.A, a.B, *val , err)
}

func client() {
	a := Args{1, 2}
	var reply int
	err := rpc.Call("192.168.1.113:4999", "test.Add", &a, &reply)
	fmt.Println("ret:", reply, err)
}

func main() {
	rpc.RegisterService("test", &Args{})
	rpc.CreateService("192.168.1.113:4999")
	go client()
	go client()
	go client()
	a := Args{1, 2}
	var reply int
	rpc.CallFunc("192.168.1.113:4999", "test.Add", &a, &reply, a.callback)
	rpc.Notify("192.168.1.113:4999", "test.ByCall", &a)
	time.Sleep(time.Second)
	rpc.StopService()
}
